/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.rsc.feeds.client.gui.richTextArea;

/**
 *
 * @author arbuckle
 */
import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.RichTextArea.Formatter;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RichTextToolbar extends Composite {
    /** Click Handler of the Toolbar **/
    private class EventHandler implements ClickHandler, KeyUpHandler, ChangeHandler {
	@Override
	public void onChange(ChangeEvent event) {
	    if (event.getSource().equals(RichTextToolbar.this.fontlist)) {
		if (isHTMLMode()) {
		    changeHtmlStyle("<span style=\"font-family: " + RichTextToolbar.this.fontlist.getValue(RichTextToolbar.this.fontlist.getSelectedIndex())
			    + ";\">", HTML_STYLE_CLOSE_SPAN);
		} else {
		    RichTextToolbar.this.styleTextFormatter
			    .setFontName(RichTextToolbar.this.fontlist.getValue(RichTextToolbar.this.fontlist.getSelectedIndex()));
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.colorlist)) {
		if (isHTMLMode()) {
		    changeHtmlStyle("<span style=\"color: " + RichTextToolbar.this.colorlist.getValue(RichTextToolbar.this.colorlist.getSelectedIndex())
			    + ";\">", HTML_STYLE_CLOSE_SPAN);
		} else {
		    RichTextToolbar.this.styleTextFormatter.setForeColor(RichTextToolbar.this.colorlist.getValue(RichTextToolbar.this.colorlist
			    .getSelectedIndex()));
		}
	    }
	}

	@Override
	public void onClick(ClickEvent event) {
	    if (event.getSource().equals(RichTextToolbar.this.bold)) {
		if (isHTMLMode()) {
		    changeHtmlStyle(HTML_STYLE_OPEN_BOLD, HTML_STYLE_CLOSE_SPAN);
		} else {
		    RichTextToolbar.this.styleTextFormatter.toggleBold();
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.italic)) {
		if (isHTMLMode()) {
		    changeHtmlStyle(HTML_STYLE_OPEN_ITALIC, HTML_STYLE_CLOSE_SPAN);
		} else {
		    RichTextToolbar.this.styleTextFormatter.toggleItalic();
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.underline)) {
		if (isHTMLMode()) {
		    changeHtmlStyle(HTML_STYLE_OPEN_UNDERLINE, HTML_STYLE_CLOSE_SPAN);
		} else {
		    RichTextToolbar.this.styleTextFormatter.toggleUnderline();
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.stroke)) {
		if (isHTMLMode()) {
		    changeHtmlStyle(HTML_STYLE_OPEN_LINETHROUGH, HTML_STYLE_CLOSE_SPAN);
		} else {
		    RichTextToolbar.this.styleTextFormatter.toggleStrikethrough();
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.subscript)) {
		if (isHTMLMode()) {
		    changeHtmlStyle(HTML_STYLE_OPEN_SUBSCRIPT, HTML_STYLE_CLOSE_SUBSCRIPT);
		} else {
		    RichTextToolbar.this.styleTextFormatter.toggleSubscript();
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.superscript)) {
		if (isHTMLMode()) {
		    changeHtmlStyle(HTML_STYLE_OPEN_SUPERSCRIPT, HTML_STYLE_CLOSE_SUPERSCRIPT);
		} else {
		    RichTextToolbar.this.styleTextFormatter.toggleSuperscript();
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.alignleft)) {
		if (isHTMLMode()) {
		    changeHtmlStyle(HTML_STYLE_OPEN_ALIGNLEFT, HTML_STYLE_CLOSE_DIV);
		} else {
		    RichTextToolbar.this.styleTextFormatter.setJustification(RichTextArea.Justification.LEFT);
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.alignmiddle)) {
		if (isHTMLMode()) {
		    changeHtmlStyle(HTML_STYLE_OPEN_ALIGNCENTER, HTML_STYLE_CLOSE_DIV);
		} else {
		    RichTextToolbar.this.styleTextFormatter.setJustification(RichTextArea.Justification.CENTER);
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.alignright)) {
		if (isHTMLMode()) {
		    changeHtmlStyle(HTML_STYLE_OPEN_ALIGNRIGHT, HTML_STYLE_CLOSE_DIV);
		} else {
		    RichTextToolbar.this.styleTextFormatter.setJustification(RichTextArea.Justification.RIGHT);
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.orderlist)) {
		if (isHTMLMode()) {
		    changeHtmlStyle(HTML_STYLE_OPEN_ORDERLIST, HTML_STYLE_CLOSE_ORDERLIST);
		} else {
		    RichTextToolbar.this.styleTextFormatter.insertOrderedList();
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.unorderlist)) {
		if (isHTMLMode()) {
		    changeHtmlStyle(HTML_STYLE_OPEN_UNORDERLIST, HTML_STYLE_CLOSE_UNORDERLIST);
		} else {
		    RichTextToolbar.this.styleTextFormatter.insertUnorderedList();
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.indentright)) {
		if (isHTMLMode()) {
		    changeHtmlStyle(HTML_STYLE_OPEN_INDENTRIGHT, HTML_STYLE_CLOSE_DIV);
		} else {
		    RichTextToolbar.this.styleTextFormatter.rightIndent();
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.indentleft)) {
		if (isHTMLMode()) {
		    //TODO nothing can be done here at the moment
		} else {
		    RichTextToolbar.this.styleTextFormatter.leftIndent();
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.generatelink)) {
		String url = Window.prompt(GUI_DIALOG_INSERTURL, "http://");
		if (url != null) {
		    if (isHTMLMode()) {
			changeHtmlStyle("<a href=\"" + url + "\">", "</a>");
		    } else {
			RichTextToolbar.this.styleTextFormatter.createLink(url);
		    }
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.breaklink)) {
		if (isHTMLMode()) {
		    //TODO nothing can be done here at the moment
		} else {
		    RichTextToolbar.this.styleTextFormatter.removeLink();
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.insertimage)) {
		String url = Window.prompt(GUI_DIALOG_IMAGEURL, "http://");
		if (url != null) {
		    if (isHTMLMode()) {
			changeHtmlStyle("<img src=\"" + url + "\">", "");
		    } else {
			RichTextToolbar.this.styleTextFormatter.insertImage(url);
		    }
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.insertline)) {
		if (isHTMLMode()) {
		    changeHtmlStyle(HTML_STYLE_HLINE, "");
		} else {
		    RichTextToolbar.this.styleTextFormatter.insertHorizontalRule();
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.removeformatting)) {
		if (isHTMLMode()) {
		    //TODO nothing can be done here at the moment
		} else {
		    RichTextToolbar.this.styleTextFormatter.removeFormat();
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.texthtml)) {
		if (RichTextToolbar.this.texthtml.isDown()) {
		    RichTextToolbar.this.styleText.setText(RichTextToolbar.this.styleText.getHTML());
		} else {
		    RichTextToolbar.this.styleText.setHTML(RichTextToolbar.this.styleText.getText());
		}
	    } else if (event.getSource().equals(RichTextToolbar.this.styleText)) {
		//Change invoked by the richtextArea
	    }
	    updateStatus();
	}

	@Override
	public void onKeyUp(KeyUpEvent event) {
	    updateStatus();
	}
    }

    /** Local CONSTANTS **/
    //ImageMap and CSS related
    private static final String HTTP_STATIC_ICONS_GIF = GWT.getModuleBaseURL() + "images/icons.gif";

    private static final String CSS_ROOT_NAME = "RichTextToolbar";
    //Color and Fontlists - First Value (key) is the Name to display, Second Value (value) is the HTML-Definition
    public final static HashMap<String, String> GUI_COLORLIST = new HashMap<String, String>();
    static {
	GUI_COLORLIST.put("White", "#FFFFFF");
	GUI_COLORLIST.put("Black", "#000000");
	GUI_COLORLIST.put("Red", "red");
	GUI_COLORLIST.put("Green", "green");
	GUI_COLORLIST.put("Yellow", "yellow");
	GUI_COLORLIST.put("Blue", "blue");
    }
    public final static HashMap<String, String> GUI_FONTLIST = new HashMap<String, String>();

    static {
	GUI_FONTLIST.put("Times New Roman", "Times New Roman");
	GUI_FONTLIST.put("Arial", "Arial");
	GUI_FONTLIST.put("Courier New", "Courier New");
	GUI_FONTLIST.put("Georgia", "Georgia");
	GUI_FONTLIST.put("Trebuchet", "Trebuchet");
	GUI_FONTLIST.put("Verdana", "Verdana");
    }
    //HTML Related (styles made by SPAN and DIV)
    private static final String HTML_STYLE_CLOSE_SPAN = "</span>";
    private static final String HTML_STYLE_CLOSE_DIV = "</div>";
    private static final String HTML_STYLE_OPEN_BOLD = "<span style=\"font-weight: bold;\">";
    private static final String HTML_STYLE_OPEN_ITALIC = "<span style=\"font-weight: italic;\">";
    private static final String HTML_STYLE_OPEN_UNDERLINE = "<span style=\"font-weight: underline;\">";
    private static final String HTML_STYLE_OPEN_LINETHROUGH = "<span style=\"font-weight: line-through;\">";
    private static final String HTML_STYLE_OPEN_ALIGNLEFT = "<div style=\"text-align: left;\">";
    private static final String HTML_STYLE_OPEN_ALIGNCENTER = "<div style=\"text-align: center;\">";
    private static final String HTML_STYLE_OPEN_ALIGNRIGHT = "<div style=\"text-align: right;\">";

    private static final String HTML_STYLE_OPEN_INDENTRIGHT = "<div style=\"margin-left: 40px;\">";
    //HTML Related (styles made by custom HTML-Tags)
    private static final String HTML_STYLE_OPEN_SUBSCRIPT = "<sub>";
    private static final String HTML_STYLE_CLOSE_SUBSCRIPT = "</sub>";
    private static final String HTML_STYLE_OPEN_SUPERSCRIPT = "<sup>";
    private static final String HTML_STYLE_CLOSE_SUPERSCRIPT = "</sup>";
    private static final String HTML_STYLE_OPEN_ORDERLIST = "<ol><li>";
    private static final String HTML_STYLE_CLOSE_ORDERLIST = "</ol></li>";
    private static final String HTML_STYLE_OPEN_UNORDERLIST = "<ul><li>";

    private static final String HTML_STYLE_CLOSE_UNORDERLIST = "</ul></li>";

    //HTML Related (styles without closing Tag)
    private static final String HTML_STYLE_HLINE = "<hr style=\"width: 100%; height: 2px;\">";
    //GUI Related stuff
    private static final String GUI_DIALOG_INSERTURL = "Enter a link URL:";

    private static final String GUI_DIALOG_IMAGEURL = "Enter an image URL:";
    private static final String GUI_LISTNAME_COLORS = "Colors";

    private static final String GUI_LISTNAME_FONTS = "Fonts";
    private static final String GUI_HOVERTEXT_SWITCHVIEW = "Switch View HTML/Source";
    private static final String GUI_HOVERTEXT_REMOVEFORMAT = "Remove Formatting";
    private static final String GUI_HOVERTEXT_IMAGE = "Insert Image";
    private static final String GUI_HOVERTEXT_HLINE = "Insert Horizontal Line";
    private static final String GUI_HOVERTEXT_BREAKLINK = "Break Link";
    private static final String GUI_HOVERTEXT_LINK = "Generate Link";
    private static final String GUI_HOVERTEXT_IDENTLEFT = "Ident Left";
    private static final String GUI_HOVERTEXT_IDENTRIGHT = "Ident Right";
    private static final String GUI_HOVERTEXT_UNORDERLIST = "Unordered List";
    private static final String GUI_HOVERTEXT_ORDERLIST = "Ordered List";
    private static final String GUI_HOVERTEXT_ALIGNRIGHT = "Align Right";
    private static final String GUI_HOVERTEXT_ALIGNCENTER = "Align Center";
    private static final String GUI_HOVERTEXT_ALIGNLEFT = "Align Left";
    private static final String GUI_HOVERTEXT_SUPERSCRIPT = "Superscript";
    private static final String GUI_HOVERTEXT_SUBSCRIPT = "Subscript";
    private static final String GUI_HOVERTEXT_STROKE = "Stroke";
    private static final String GUI_HOVERTEXT_UNDERLINE = "Underline";
    private static final String GUI_HOVERTEXT_ITALIC = "Italic";

    private static final String GUI_HOVERTEXT_BOLD = "Bold";

    /** Native JavaScript that returns the selected text and position of the start **/
    public static native JsArrayString getSelection(Element elem) /*-{
        var txt = "";
        var pos = 0;
        var range;
        var parentElement;
        var container;

        if (elem.contentWindow.getSelection) {
        txt = elem.contentWindow.getSelection();
        pos = elem.contentWindow.getSelection().getRangeAt(0).startOffset;
        } else if (elem.contentWindow.document.getSelection) {
        txt = elem.contentWindow.document.getSelection();
        pos = elem.contentWindow.document.getSelection().getRangeAt(0).startOffset;
        } else if (elem.contentWindow.document.selection) {
        range = elem.contentWindow.document.selection.createRange();
        txt = range.text;
        parentElement = range.parentElement();
        container = range.duplicate();
        container.moveToElementText(parentElement);
        container.setEndPoint('EndToEnd', range);
        pos = container.text.length - range.text.length;
        }
        return [""+txt,""+pos];
    }-*/;

    /** Private Variables **/
    //The main (Vertical)-Panel and the two inner (Horizontal)-Panels
    private VerticalPanel outer;

    private FlexTable topPanel;
    private HorizontalPanel bottomPanel;

    //The RichTextArea this Toolbar referes to and the Interfaces to access the RichTextArea
    private RichTextArea styleText;

    private Formatter styleTextFormatter;
    //We use an internal class of the ClickHandler and the KeyUpHandler to be private to others with these events
    private EventHandler evHandler;
    //The Buttons of the Menubar
    private ToggleButton bold;
    private ToggleButton italic;
    private ToggleButton underline;
    private ToggleButton stroke;
    private ToggleButton subscript;
    private ToggleButton superscript;
    private PushButton alignleft;
    private PushButton alignmiddle;
    private PushButton alignright;
    private PushButton orderlist;
    private PushButton unorderlist;
    private PushButton indentleft;
    private PushButton indentright;
    private PushButton generatelink;
    private PushButton breaklink;
    private PushButton insertline;
    private PushButton insertimage;

    private PushButton removeformatting;
    private ToggleButton texthtml;

    private ListBox fontlist;

    private ListBox colorlist;

    /** Constructor of the Toolbar **/
    public RichTextToolbar(RichTextArea richtext) {
	//Initialize the main-panel
	this.outer = new VerticalPanel();

	//Initialize the two inner panels
	this.topPanel = new FlexTable();
	this.topPanel.setStyleName(CSS_ROOT_NAME);
	this.bottomPanel = new HorizontalPanel();
	this.bottomPanel.setStyleName(CSS_ROOT_NAME);

	//Save the reference to the RichText area we refer to and get the interfaces to the stylings

	this.styleText = richtext;
	this.styleTextFormatter = this.styleText.getFormatter();

	//Set some graphical options, so this toolbar looks how we like it.
	//this.topPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
	this.bottomPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);

	//Add the two inner panels to the main panel
	this.outer.add(this.topPanel);
	this.outer.add(this.bottomPanel);

	//Some graphical stuff to the main panel and the initialisation of the new widget
	this.outer.setWidth("100%");
	this.outer.setStyleName(CSS_ROOT_NAME);
	initWidget(this.outer);

	//
	this.evHandler = new EventHandler();

	//Add KeyUp and Click-Handler to the RichText, so that we can actualize the toolbar if neccessary
	this.styleText.addKeyUpHandler(this.evHandler);
	this.styleText.addClickHandler(this.evHandler);

	//Now lets fill the new toolbar with life
	buildTools();
    }

    /** Initialize the options on the toolbar **/
    private void buildTools() {
	//Init the TOP Panel forst
	int row = 0;
	this.topPanel.setWidget(row, 0, this.bold = createToggleButton(HTTP_STATIC_ICONS_GIF, 0, 0, 20, 20, GUI_HOVERTEXT_BOLD));
	this.topPanel.setWidget(row, 1, this.italic = createToggleButton(HTTP_STATIC_ICONS_GIF, 0, 60, 20, 20, GUI_HOVERTEXT_ITALIC));
	this.topPanel.setWidget(row, 2, this.underline = createToggleButton(HTTP_STATIC_ICONS_GIF, 0, 140, 20, 20, GUI_HOVERTEXT_UNDERLINE));
	this.topPanel.setWidget(row, 3, this.stroke = createToggleButton(HTTP_STATIC_ICONS_GIF, 0, 120, 20, 20, GUI_HOVERTEXT_STROKE));
	this.topPanel.setWidget(row, 4, this.alignleft = createPushButton(HTTP_STATIC_ICONS_GIF, 0, 460, 20, 20, GUI_HOVERTEXT_ALIGNLEFT));
	this.topPanel.setWidget(row, 5, this.alignmiddle = createPushButton(HTTP_STATIC_ICONS_GIF, 0, 420, 20, 20, GUI_HOVERTEXT_ALIGNCENTER));
	this.topPanel.setWidget(row, 6, this.alignright = createPushButton(HTTP_STATIC_ICONS_GIF, 0, 480, 20, 20, GUI_HOVERTEXT_ALIGNRIGHT));
	row++;
	/*this.topPanel.setWidget(row, 0, this.subscript = createToggleButton(HTTP_STATIC_ICONS_GIF, 0, 600, 20, 20, GUI_HOVERTEXT_SUBSCRIPT));
	this.topPanel.setWidget(row, 1, this.superscript = createToggleButton(HTTP_STATIC_ICONS_GIF, 0, 620, 20, 20, GUI_HOVERTEXT_SUPERSCRIPT));*/

	this.topPanel.setWidget(row, 0, this.orderlist = createPushButton(HTTP_STATIC_ICONS_GIF, 0, 80, 20, 20, GUI_HOVERTEXT_ORDERLIST));
	this.topPanel.setWidget(row, 1, this.unorderlist = createPushButton(HTTP_STATIC_ICONS_GIF, 0, 20, 20, 20, GUI_HOVERTEXT_UNORDERLIST));
	this.topPanel.setWidget(row, 2, this.indentright = createPushButton(HTTP_STATIC_ICONS_GIF, 0, 400, 20, 20, GUI_HOVERTEXT_IDENTRIGHT));
	this.topPanel.setWidget(row, 3, this.indentleft = createPushButton(HTTP_STATIC_ICONS_GIF, 0, 540, 20, 20, GUI_HOVERTEXT_IDENTLEFT));
	this.topPanel.setWidget(row, 4, this.generatelink = createPushButton(HTTP_STATIC_ICONS_GIF, 0, 500, 20, 20, GUI_HOVERTEXT_LINK));
	this.topPanel.setWidget(row, 5, this.breaklink = createPushButton(HTTP_STATIC_ICONS_GIF, 0, 640, 20, 20, GUI_HOVERTEXT_BREAKLINK));
	row++;
	this.topPanel.setWidget(row, 0, this.insertline = createPushButton(HTTP_STATIC_ICONS_GIF, 0, 360, 20, 20, GUI_HOVERTEXT_HLINE));
	this.topPanel.setWidget(row, 1, this.insertimage = createPushButton(HTTP_STATIC_ICONS_GIF, 0, 380, 20, 20, GUI_HOVERTEXT_IMAGE));
	this.topPanel.setWidget(row, 2, this.removeformatting = createPushButton(HTTP_STATIC_ICONS_GIF, 20, 460, 20, 20, GUI_HOVERTEXT_REMOVEFORMAT));
	this.topPanel.setWidget(row, 3, this.texthtml = createToggleButton(HTTP_STATIC_ICONS_GIF, 0, 260, 20, 20, GUI_HOVERTEXT_SWITCHVIEW));

	//Init the BOTTOM Panel
	this.bottomPanel.add(this.fontlist = createFontList());
	this.bottomPanel.add(new HTML("&nbsp;"));
	this.bottomPanel.add(this.colorlist = createColorList());
    }

    /** Method called to toggle the style in HTML-Mode **/
    private void changeHtmlStyle(String startTag, String stopTag) {
	JsArrayString tx = getSelection(this.styleText.getElement());
	String txbuffer = this.styleText.getText();
	Integer startpos = Integer.parseInt(tx.get(1));
	String selectedText = tx.get(0);
	this.styleText.setText(txbuffer.substring(0, startpos) + startTag + selectedText + stopTag + txbuffer.substring(startpos + selectedText.length()));
    }

    /** Method to create the colorlist for the toolbar **/
    private ListBox createColorList() {
	ListBox mylistBox = new ListBox();
	mylistBox.addChangeHandler(this.evHandler);
	mylistBox.setVisibleItemCount(1);

	mylistBox.addItem(GUI_LISTNAME_COLORS);
	for (String name : GUI_COLORLIST.keySet()) {
	    mylistBox.addItem(name, GUI_COLORLIST.get(name));
	}

	return mylistBox;
    }

    /** Method to create the fontlist for the toolbar **/
    private ListBox createFontList() {
	ListBox mylistBox = new ListBox();
	mylistBox.addChangeHandler(this.evHandler);
	mylistBox.setVisibleItemCount(1);

	mylistBox.addItem(GUI_LISTNAME_FONTS);
	for (String name : GUI_FONTLIST.keySet()) {
	    mylistBox.addItem(name, GUI_FONTLIST.get(name));
	}

	return mylistBox;
    }

    /** Method to create a Push button for the toolbar **/
    private PushButton createPushButton(String url, Integer top, Integer left, Integer width, Integer height, String tip) {
	Image extract = new Image(url, left, top, width, height);
	PushButton tb = new PushButton(extract);
	tb.setHeight(height + "px");
	tb.setWidth(width + "px");
	tb.addClickHandler(this.evHandler);
	if (tip != null) {
	    tb.setTitle(tip);
	}
	return tb;
    }

    /** Method to create a Toggle button for the toolbar **/
    private ToggleButton createToggleButton(String url, Integer top, Integer left, Integer width, Integer height, String tip) {
	Image extract = new Image(url, left, top, width, height);
	ToggleButton tb = new ToggleButton(extract);
	tb.setHeight(height + "px");
	tb.setWidth(width + "px");
	tb.addClickHandler(this.evHandler);
	if (tip != null) {
	    tb.setTitle(tip);
	}
	return tb;
    }

    /** Private method with a more understandable name to get if HTML mode is on or not **/
    private Boolean isHTMLMode() {
	return this.texthtml.isDown();
    }

    /** Private method to set the toggle buttons and disable/enable buttons which do not work in html-mode **/
    private void updateStatus() {
	if (this.styleTextFormatter != null) {
	    this.bold.setDown(this.styleTextFormatter.isBold());
	    this.italic.setDown(this.styleTextFormatter.isItalic());
	    this.underline.setDown(this.styleTextFormatter.isUnderlined());
	    this.subscript.setDown(this.styleTextFormatter.isSubscript());
	    this.superscript.setDown(this.styleTextFormatter.isSuperscript());
	    this.stroke.setDown(this.styleTextFormatter.isStrikethrough());
	}

	if (isHTMLMode()) {
	    this.removeformatting.setEnabled(false);
	    this.indentleft.setEnabled(false);
	    this.breaklink.setEnabled(false);
	} else {
	    this.removeformatting.setEnabled(true);
	    this.indentleft.setEnabled(true);
	    this.breaklink.setEnabled(true);
	}
    }

}
